import utils from "../../utils/index";
import {rankModel} from "../../apis/index"

import regeneratorRuntime from '../../libs/wxPromise.min';
// pages/rank/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [{name:'个人榜'},{name:'粉丝团榜'}],
    tabIndex: 0,
    page1:{
      page:1,
      pageSize:10
    },
    singleList:[],
    page2:{
      page:1,
      pageSize:10
    },
    teamList:[],
  },
  onLoad: function (options) {
    let {sid,tabIndex} = options
    this.data.sid = sid?sid:""
    tabIndex = tabIndex?tabIndex:0
    this.setData({
      tabIndex:tabIndex
    })
  },
  mixins: [require('../../myMixin.js')],
  async onShow () {
    this.getInitStarList()
  },
  tabClick(e){
    let index = e.detail.index
    this.setData({
      tabIndex:index
    })
    this.getInitStarList()
  },
  goUrl(e){
    let url = e.currentTarget.dataset.url
    utils.goUrl(url)
  },
  getInitStarList(){
    let {singleList,teamList,page1,page2,tabIndex} = this.data
    if(tabIndex==0){
      this.data.page1.page = 1
      this.data.singleList = []
      this.getSingleContributes()
    }else if(tabIndex==1){
      this.data.page2.page = 1
      this.data.teamList = []
      this.getTeamleContributes()
    }

  },
  getSingleContributes(){
    let {page,pageSize} = this.data.page1
    let {singleList,sid} = this.data
    let param = {
      page:page,
      pageSize:pageSize,
      sid:sid
    }
    utils.showLoading()
    rankModel.singleContributes(param).then(res=>{
      let _list = res.list
      singleList = singleList.concat(_list)
      this.computedPage('page1',res.total)
      this.setData({
        singleList:singleList
      })  
      utils.hideAll()
    }).catch(()=>{
      utils.hideAll()
    })
  },
  moreList1Data(){
    this.loadMoreData('page1',this.getSingleContributes)
  },
  getTeamleContributes(){
    let {page,pageSize} = this.data.page2
    let {teamList,sid} = this.data
    let param = {
      page:page,
      pageSize:pageSize,
      sid:sid
    }
    utils.showLoading()
    rankModel.teamContributes(param).then(res=>{
      let _list = res.list
      teamList = teamList.concat(_list)
      this.computedPage('page2',res.total)
      this.setData({
        teamList:teamList
      })  
      utils.hideAll()
    }).catch(()=>{
      utils.hideAll()
    })
  },
  moreList2Data(){
    this.loadMoreData('page2',this.getSingleContributes)
  }
})